package edu.cmu.cs.erl.unitTests;

import java.util.*;

import jp.ac.kobe_u.cs.prolog.lang.JavaObjectTerm;
import jp.ac.kobe_u.cs.prolog.lang.Term;
import jp.ac.kobe_u.cs.prolog.lang.VariableTerm;
import edu.cmu.cs.erl.ERLException;
import edu.cmu.cs.erl.Oracle;

public class TestOracle implements Oracle {
	private Hashtable<String, VariableTerm> map = new Hashtable<String, VariableTerm>();


	public boolean evaluate(Hashtable<String, Object> context, Object name) {
		assert(name instanceof String);
		if (name.equals("a"))
			return true;
		else if (name.equals("b"))
			return false;
		else if (name.equals("c"))
			return true;
		else
			return false;
	}

	public List<Object> getObjects(Hashtable<String, Object> context, Object groundedTerm) {
		return (List<Object>)groundedTerm;
	}
	
	public String getMessage(Hashtable<String, Object> context, Object atomicTerm) {
		if (atomicTerm.equals("a"))
			return "a must be true";
		else if (atomicTerm.equals("b"))
			return "b must be true";
		else if (atomicTerm.equals("c"))
			return "c must be true";
		else if (atomicTerm instanceof VariableTerm) {
			for (Map.Entry<String, VariableTerm> entry : map.entrySet()) {
				if (entry.getValue().equals(atomicTerm))
					return entry.getKey();
			}
			return "";
		}
		else
			return atomicTerm.toString();
	}
	
	public String getNotMessage(Hashtable<String, Object> context, Object atomicTerm) {
		if (atomicTerm.equals("a"))
			return "a must be false";
		else if (atomicTerm.equals("b"))
			return "b  must be false";
		else if (atomicTerm.equals("c"))
			return "c must be false";
		else
			return atomicTerm.toString();
	}
	
	public String getIsMessage(Hashtable<String, Object> context, Object atomicTerm){
		Object eq = findEq(context, evaluate(context,atomicTerm));
		System.out.println("at in is: "+atomicTerm);
		return atomicTerm+" is equal to "+eq;
	}	
	public String getIsNotMessage(Hashtable<String, Object> context, Object atomicTerm){
		Object neq = findEq(context,!evaluate(context,atomicTerm));
		return atomicTerm+" is not equal to "+neq; 
	}
	private Object findEq(Hashtable<String, Object> context, boolean tomatch){
		ArrayList possible = new ArrayList<Object>(context.values());
		for(int a=0;a<possible.size();a++){
			if(evaluate(context,possible.get(a))==tomatch)
				return possible.get(a);
		}
		return null;
	}

	public VariableTerm getVariable(String str) {
		if (map.containsKey(str)) {
			return map.get(str);
		}
		else {
			VariableTerm tm =  new VariableTerm();
			map.put(str, tm);
			return tm;
		}
	}

	public Hashtable<String, Object> getStartingContext() {
		return new Hashtable<String, Object>();
	}

	public void addToStartingContext(String name, VariableTerm variable) {
		map.put(name, variable);
		
	}
}
